//
//  ContainerView.m
//  ActiveDeck
//
//  Created by Laura McBride on 9/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContainerView.h"
#import "AD_ContainerOB.h"
#import "AD_Notice.h"
#import "AD_Note.h"
#import "AD_PhotoNote.h"


@implementation ContainerView
@synthesize bt_Notices, bt_NoteBook, bt_Note,bt_Photo,bt_AskQes , UI_NoteScroll, lb_Notices, UI_NoticeScroll, bt_ShowContainer,Vw_ScrollItem;
@synthesize Vw_Container,Img_BG,_delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:@"Container" owner:self options:nil];
        [Vw_Container sizeThatFits:frame.size];
        
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            bt_Photo.hidden = TRUE;

        
        [self addSubview:Vw_Container];
    }
    return self;
}

- (IBAction)AddNote
{
    [_delegate AddNote];
}

- (void)EditNote:(int)SlideIndex
{
    [_delegate EditNote:SlideIndex];
}


- (IBAction)ShowNotice
{
//    Img_BG.image = [UIImage imageNamed:@"bg_container_up.png"];

    UI_NoticeScroll.hidden = FALSE;
    UI_NoteScroll.hidden = TRUE;
}

- (IBAction)ShowNote
{
//    if(UI_NoteScroll.contentSize.width < 1)
//        Img_BG.image = [UIImage imageNamed:@"bg_container_directions.png"];
//    else
//        Img_BG.image = [UIImage imageNamed:@"bg_container_up.png"];
    
    UI_NoticeScroll.hidden = TRUE;
    UI_NoteScroll.hidden = FALSE;
}

- (IBAction)ShowMoreContainer
{
    [_delegate ShowMoreContainer];
}

-(IBAction)MaxContainer
{
    [_delegate MaxContainer];
}

- (IBAction)ShowCamera
{
    [_delegate UserCamera];
}

- (void)EditPhotoNote:(int)SlideIndex
{
    [_delegate EditPhotoNote:SlideIndex];
}


#define textBlockWidth 247
#define textBlockHeight 160

#define scrollVWidth 741
#define scrollVHeight 160

- (void)initWithContainOB:(AD_ContainerOB*)containOB
               totalSlide:(int)totalSlide
{
    NSArray* ArrNotices = containOB.Array_Notice;

    UI_NoticeScroll.contentSize = CGSizeMake([ArrNotices count] * textBlockWidth, scrollVHeight);
    UI_NoticeScroll.showsHorizontalScrollIndicator = YES;
    UI_NoticeScroll.showsVerticalScrollIndicator = NO;
    UI_NoticeScroll.scrollEnabled = YES;
    UI_NoticeScroll.delegate = self;
    UI_NoticeScroll.bounces = YES;
    
    int StartX = 0;
    
    for (AD_Notice *adNotice in ArrNotices) {
        UIView *VW_Notice = [self ScrollViewItemNotice:adNotice];
        VW_Notice.frame = CGRectMake(StartX, 0, textBlockWidth, textBlockHeight);
        
        [UI_NoticeScroll addSubview:VW_Notice];

        StartX += textBlockWidth;
    }
    
    NSLog(@"totalSlideCount = %d" , totalSlide);

    
    UI_NoticeScroll.hidden = TRUE;    
    
    NSArray *ArrNotes = containOB.Array_Note;
    NSArray *ArrPhotos = containOB.Array_Photo;
    
    int noteCount = [ArrNotes count] + [ArrPhotos count];
    
    UI_NoteScroll.contentSize = CGSizeMake(noteCount * textBlockWidth, scrollVHeight);
    UI_NoteScroll.showsHorizontalScrollIndicator = YES;
    UI_NoteScroll.showsVerticalScrollIndicator = NO;
    UI_NoteScroll.scrollEnabled = YES;
    UI_NoteScroll.delegate = self;
    UI_NoteScroll.bounces = YES;
    
    StartX = 0;

    for (int i=0; i< totalSlide; i++) {
        for (AD_Note *adNote in ArrNotes)
        {
            if(adNote.SlideIndex == i)
            {
                UIView *VW_Note = [self ScrollViewItem:adNote];
                VW_Note.frame = CGRectMake(StartX, 0, textBlockWidth, textBlockHeight);
                [UI_NoteScroll addSubview:VW_Note];
                StartX += textBlockWidth;
                break;
            }
        }
        
        for (AD_PhotoNote *adPhoto in ArrPhotos)
        {
            if(adPhoto.SlideIndex == i)
            {
                UIView *VW_Photo = [self ScrollViewItemPhoto:adPhoto DocumentPath:containOB.Path_Container];
                VW_Photo.frame = CGRectMake(StartX, 0, textBlockWidth, textBlockHeight);
                [UI_NoteScroll addSubview:VW_Photo];
                StartX += textBlockWidth;
                break;
            }
        }
    }
    
}

-(UIView*)ScrollViewItemNotice:(AD_Notice *)adNotice
{
    [[NSBundle mainBundle] loadNibNamed:@"Scroll_Items" owner:self options:NULL];
    UIView *scrollItem = Vw_ScrollItem;
    UILabel *lab_Notice = (UILabel *)[scrollItem viewWithTag:1];
    UITextView *TV_Notice = (UITextView *)[scrollItem viewWithTag:2];
    
    lab_Notice.text = adNotice.Txt_Time;
    TV_Notice.text = adNotice.Txt_Notice;
    
    return scrollItem;
}

-(UIView*)ScrollViewItem:(AD_Note *)adNote
{
    [[NSBundle mainBundle] loadNibNamed:@"Scroll_Items_Note" owner:self options:NULL];
    UIView *scrollItem = Vw_ScrollItem;
    UILabel *lab_Note = (UILabel*)[scrollItem viewWithTag:1];
    UITextView *TV_Note = (UITextView *)[scrollItem viewWithTag:2];
    UIButton *Bt_Edit = (UIButton *)[scrollItem viewWithTag:3];
    
    Bt_Edit.tag = adNote.SlideIndex;
    [Bt_Edit addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    
    lab_Note.text = [NSString stringWithFormat:@"SLIDE %d / NOTE" , adNote.SlideIndex + 1];
    TV_Note.text = adNote.Txt_Note;
//    TV_Note.scrollEnabled = FALSE;
    return scrollItem;
}

-(UIView*)ScrollViewItemPhoto:(AD_PhotoNote *)adPhoto
                 DocumentPath:(NSString *)DocumentPath
{
    [[NSBundle mainBundle] loadNibNamed:@"Scroll_ItemsPhoto" owner:self options:NULL];
    UIView *scrollItem = Vw_ScrollItem;
    UILabel *lab_Note = (UILabel*)[scrollItem viewWithTag:1];
    UITextView *TV_Note = (UITextView *)[scrollItem viewWithTag:2];
    UIImageView *Img_Photo = (UIImageView *)[scrollItem viewWithTag:3];
    UIButton *Bt_Edit = (UIButton *)[scrollItem viewWithTag:4];

    
    Bt_Edit.tag = adPhoto.SlideIndex;
    [Bt_Edit addTarget:self action:@selector(buttonPhotoPress:) forControlEvents:UIControlEventTouchUpInside];
    
    
    lab_Note.text = [NSString stringWithFormat:@"SLIDE %d / PHOTO" , adPhoto.SlideIndex + 1];
    TV_Note.text = adPhoto.Txt_Note;
    Img_Photo.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",DocumentPath,  adPhoto.Path_Image]]];

    return scrollItem;
}


- (void)buttonPress:(id)sender
{
    UIButton *pressButton = (UIButton *)sender;
    [self EditNote:pressButton.tag];
}


- (void)buttonPhotoPress:(id)sender
{
    UIButton *pressButton = (UIButton *)sender;
    [self EditPhotoNote:pressButton.tag];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [Vw_Container release];
    [bt_ShowContainer release];
    [bt_AskQes release];
    [bt_Note release];
    [bt_NoteBook release];
    [bt_Notices release];
    [bt_Photo release];
    [Vw_ScrollItem release];
    [Img_BG release];
    [super dealloc];

}

@end
